//
// Created by Semir on 2020/5/7.
//

#include <iostream>
#include <cstring>

using namespace std;

void replaceSpace(char *str, int length) {
    if (str == nullptr || length <= 0) {
        return;
    }
    int charLen = 0;
    int cnt = 0;
    for (; str[charLen] != '\0'; charLen++) {
        if (str[charLen] == ' ') {
            cnt++;
        }
    }

    cnt = cnt << 1;
    str[charLen + cnt] = '\0';
    int i = charLen - 1;
    int j = charLen + cnt - 1;
    while (i >= 0) {
        if (str[i] == ' ') {
            str[j--] = '0';
            str[j--] = '2';
            str[j--] = '%';
            i--;
        } else {
            str[j--] = str[i--];
        }
    }
}

void Test() {
    char str[20];
    memset(str, '\0', sizeof(str));
    str[0] = 'A';
    str[1] = ' ';
    str[2] = 'B';
    replaceSpace(str, 20);
    int i = 0;
    while (str[i] != '\0') {
        cout << str[i];
        i++;
    }
    cout << endl;
}

int main() {
    Test();
    return 0;
}
